package com.dgty.dg_common.util

import com.blankj.utilcode.util.GsonUtils
import com.dgty.dg_common.bean.LoginBean


/**
 * Title: UserUtil
 * Description:
 * Created DateTime: 2023-03-14 11:31
 * Created by xxl.
 */
class UserUtil {


    val USER_INFO: String = "user_info"
    val USER_TOKEN_GOOGLE: String = "user_google_idtoken"
    val USER_TOKEN_TEMP: String = "user_temp_token"

    companion object {
        var isLogin = false //登录状态记录
        val sInstance by lazy(LazyThreadSafetyMode.NONE) {
            UserUtil()
        }
    }

    fun saveUser(userinfo: String) {
        KVUtil.put(USER_INFO, userinfo)
        isLogin = true
    }

    fun saveGoogleToken(idtoken: String) {
        KVUtil.put(USER_TOKEN_GOOGLE, idtoken)
    }
    fun saveTempToken(tempToken: String) {
        KVUtil.put(USER_TOKEN_TEMP, tempToken)
    }

    fun getTempToken(): String? {
        return KVUtil.getString(USER_TOKEN_TEMP)
    }
    fun getGoogleToken(): String? {
        return KVUtil.getString(USER_TOKEN_GOOGLE)
    }


    fun getUser(): LoginBean? {
        var user = KVUtil.getString(USER_INFO, "")
        return GsonUtils.fromJson(user, LoginBean::class.java)
    }
    /* private fun getUser(): String? {
         var user = KVUtil.getString(USER_INFO, "")
         return user
     }*/

    /**
     * 推出登录和登录失败的时候调用
     */
    fun cleanUserInfo() {
        isLogin = false
        KVUtil.put(USER_INFO, "")
    }
}